package com.west_traveler.find_page_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_anchor.EntityAnchor;
import com.west_traveler.entity.entity_anchor.Item_list_Anchor;
import com.west_traveler.inteface.HttpService;
import com.west_traveler.recyclerview_adapter.AnchorFraRecyclerViewAdapte;
import com.west_traveler.ximalayafm.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Description: 发现页主播Fragment
 * Date:2016年12月02日15:10
 *
 * @author: 张景洋.
 */

public class AnchorFragment extends Fragment implements Callback<EntityAnchor> {


    private RecyclerView recycler;
    private AnchorFraRecyclerViewAdapte adapter;
    private boolean loading ;
    private HttpService service;
    private int page = 1;

    public static AnchorFragment getInstance(){
        return new AnchorFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_anchor, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler = (RecyclerView) view.findViewById(R.id.recycleview_anchor_Fra_id);
        //数据源
        BaseApplication application = (BaseApplication) getActivity().getApplication();
        service = application.getService();
        service.getAnchorList("android",1).enqueue(this);

        adapter = new AnchorFraRecyclerViewAdapte(getContext());
        recycler.setAdapter(adapter);
        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                layoutManager.getItemCount() 可以得到当前RecyclerView中Item的总数目
//                layoutManager.findLastVisibleItemPosition() 得到最后一个可见的Item的位置position
            //    Log.i("100","进入滑动判断");
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int totalItemCount = layoutManager.getItemCount();
                int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
             //   Log.i("100","itemtotal="+totalItemCount);
             //   Log.i("100","lastVisibleItem="+lastVisibleItem);
                if(!loading && totalItemCount==lastVisibleItem+1){
                //    Log.i("100","进入条目判断.........................................");
                    loading=true;
                    service.getAnchorList("android",++page).enqueue(new Callback<EntityAnchor>() {
                        @Override
                        public void onResponse(Call<EntityAnchor> call, Response<EntityAnchor> response) {

                            List<Item_list_Anchor> anchorlist = response.body().getAnchorlist();
                            adapter.addAll(anchorlist);
                        //    Log.i("100","加载完毕第"+page);
                            loading=false;
                            if (page==3){
                                loading=true;
                            }
                          //  Log.i("100","loading=....................."+loading);
                        }
                        @Override
                        public void onFailure(Call<EntityAnchor> call, Throwable t) {

                        }
                    });


                }
            }
        });

    }

    @Override
    public void onResponse(Call<EntityAnchor> call, Response<EntityAnchor> response) {

        List<Item_list_Anchor> anchorlist = response.body().getAnchorlist();
        adapter.addAll(anchorlist);
    }

    @Override
    public void onFailure(Call<EntityAnchor> call, Throwable t) {
    }


}
